<?php
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | NewThink [ Think More,Think Better! ]
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | 版权所有：山西创泰网络科技有限公司
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Author: lk Date:2019/7/30 Time:10:57
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

namespace app\api\controller;

use app\common\service\companyIndustry\industrymodel\TsinghuaDrainEmissionToFacilityParam;
use app\common\service\WorkingModelService;

use think\Db;

class ControlFive extends BaseController
{
    /**
     * 获取详情
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function particulars()
    {
        $param = $this->request->param();
        
        if (empty($param['plan_number']) || empty($param['company_id']) || empty($param['facility_id'])) {
            $this->error('参数错误');
        }
        
        $tsinghuaPlanName = $this->getTsinghuaPlanName($param['plan_number']);#获取方案名称
        $facilityAlias    = $this->getFacilityAlias($param['facility_id']);#获取治理设施别名
        $times            = $this->getControlTime($param['plan_number']);#查询获取管控时间
        
        $data = [];
        
        #运行浓度和运行效率
        $tsinghua                       = new TsinghuaDrainEmissionToFacilityParam;
        $planConcentrationAndEfficiency = $tsinghua->getPlanParamByFacilityId($param['facility_id'], $param['plan_number'], $tsinghuaPlanName, $facilityAlias);
        $data                           = array_merge($data, $planConcentrationAndEfficiency);
        
        #获取供浆量、循环泵组合、PH值、喷氨量（脱硝脱硫,类型不同获取不同）
        $ws           = new WorkingModelService;
        $forecastData = $ws->forecast($param['company_id'], $param['facility_id'], $times['startTime'], $times['endTime'], $param['plan_number'], $tsinghuaPlanName);
        isset($forecastData['slurrySupply']) && $forecastData['slurrySupply'] = $forecastData['slurrySupply'] * 60;
        isset($forecastData['NH3InVol']) && $forecastData['NH3InVol'] = $forecastData['NH3InVol'] * 60;
        $data = array_merge($data, $forecastData);
        
        #获取脱硫数据
        if ($facilityAlias == 'SO2') {
            #获取液气和比钙硫比
            $ws           = new WorkingModelService;
            $reactionData = $ws->reaction($param['company_id'], $param['facility_id'], $times['startTime'], $times['endTime'], $param['plan_number'], $tsinghuaPlanName);
            $data         = array_merge($data, $reactionData);
        }
        
        $this->success('成功', $data);
    }
    
    /**
     * 获取方案名称
     * @param $planNumber
     * @return mixed
     * @throws \Exception
     */
    private function getTsinghuaPlanName($planNumber)
    {
        $sql    = "SELECT `name` FROM `control_plan_from_tsinghua_plan` WHERE `plan_number` = ? AND `confirm_scheme`= 1";
        $result = Db::query($sql, array($planNumber));
        if (empty($result)) {
            throw new \Exception('未知方案');
        }
        return $result[0]['name'];
    }
    
    /**
     * 通过预案号获取管控前七天
     * @param $planNumber
     * @return array
     */
    private function getControlTime($planNumber): array
    {
        $sql         = "SELECT `control_start_time`, `control_end_time` FROM `control_plan_name` WHERE `plan_number` = ? ";
        $controlTime = Db::query($sql, array($planNumber));
        if (empty($controlTime)) {
            $this->error('未知预案号!');
        }
        $controlStartTime = strtotime($controlTime[0]['control_start_time']);
        $result           = [
            'startTime' => date('Ymd', strtotime('-7 day', $controlStartTime)),
            'endTime'   => date('Ymd', strtotime('-1 day', $controlStartTime)),
        ];
        
        return array_merge($result, $controlTime[0]);
    }
    
    /**
     * 获取治理设施别名 SO2/NO2
     * @param $facilityId
     * @return string
     */
    private function getFacilityAlias($facilityId): string
    {
        $sql      = "SELECT `type` FROM `basic_facility` WHERE id = ?";
        $facility = Db::query($sql, array($facilityId));
        if (empty($facility)) {
            $this->error('未知设施');
        }
        $facilityType = $facility[0]['type'];
        
        $typeAlias = "";
        switch ($facilityType) {
            case 1:
                $typeAlias = "NO2";
                break;
            case 2:
                $typeAlias = "SO2";
                break;
        }
        
        return $typeAlias;
    }
}